/***************************************************
	FileName: sshell.c
	Purpose: Execute all unix shell commands through the executable file.
	Description: Ties in all the individual pieces of the shell utility
        that we are creating.
	Modified by Grant Meyers
***************************************************/
#include "parser.h"
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int main(void) {
	char input[MAXINPUTLINE];
	signal_c_init();

	//assignment 6
	if(atexit(keylog_exit1)!=0) {
		perror("Failed to attach atexit().  sshell.c line 20.");
		exit(EXIT_FAILURE);
	}
	///
	if(printf("Welcome to the sample shell!  You may enter commands here, one\n")<0)  {
		perror("printf error.  sshell.c line 25");
		exit(EXIT_FAILURE);
	}
	if(printf("per line.  When you're finished, press Ctrl+D on a line by\n")<0)  {
		perror("printf error.  sshell.c line 29");
		exit(EXIT_FAILURE);
	}
	if(printf("itself.  I understand basic commands and arguments separated by\n")<0)  {
		perror("printf error.  sshell.c line 33");
		exit(EXIT_FAILURE);
	}
	if(printf("spaces, redirection with < and >, up to two commands joined\n")<0)  {
		perror("printf error.  sshell.c line 37");
		exit(EXIT_FAILURE);
	}
	if(printf("by a pipe, tilde expansion, and background commands with &.\n\n")<0)  {
		perror("printf error.  sshell.c line 41");
		exit(EXIT_FAILURE);
	}

	if(printf("\nGMsh$ ")<0)  {
		perror("printf error.  sshell.c line 46");
		exit(EXIT_FAILURE);
	}

	char* fgets_ret = 0;

	while ((fgets_ret = fgets(input, sizeof(input), stdin))) {
		if(fgets_ret == NULL) {
			perror("Error fgets returned null.  sshell.c line 54");
			exit(EXIT_FAILURE);
		}
		stripcrlf(input);
		parse(input);
		if(printf("\nGMsh$ ")<0)  {
			perror("printf error.  sshell.c line 60");
			exit(EXIT_FAILURE);
		}
	}
	return 0;
}
